package com.example.tool.otherui;

import javafx.util.StringConverter;

import java.time.LocalTime;
import java.time.format.FormatStyle;
import java.util.Locale;

public class LocalTimeStringConverter2 extends StringConverter<LocalTime> {

    LocalTimeStringConverter2.LdtConverter ldtConverter;

    // ------------------------------------------------------------ Constructors

    /**
     * Create a {@link StringConverter} for {@link LocalTime} values, using a
     * default formatter and parser with {@link FormatStyle#SHORT}, and the
     * user's {@link Locale}.
     */
    public LocalTimeStringConverter2() {
        ldtConverter = new LocalTimeStringConverter2.LdtConverter();
    }

    // ------------------------------------------------------- Converter Methods

    /**
     * {@inheritDoc}
     */
    @Override
    public LocalTime fromString(String value) {
        return ldtConverter.fromString(value);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString(LocalTime value) {
        return ldtConverter.toString(value);
    }


    static class LdtConverter extends StringConverter<LocalTime> {


        LdtConverter() {
        }

        /**
         * {@inheritDoc}
         */
        @SuppressWarnings({"unchecked"})
        @Override
        public LocalTime fromString(String text) {
            if (text == null || text.isEmpty()) {
                return null;
            }

            text = text.trim();
            String[] arr = text.split(":");


            return LocalTime.of(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]));
        }


        /**
         * {@inheritDoc}
         */
        @Override
        public String toString(LocalTime value) {
            // If the specified value is null, return a zero-length String
            return value.toString();
        }


    }
}
